package com.openness.spider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.hadoop.hbase.client.Put;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openness.spider.commons.GeneralBuffer;
import com.openness.spider.commons.Statics;
import com.openness.spider.hbase.HBaseUtil;

/**
 * @author Rain
 * @version 2013-6-21 上午9:46:34
 */

public class HtmlBuffer extends GeneralBuffer<Html> {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(HtmlBuffer.class);

	public HtmlBuffer(SpiderConfig config) {
		super(config);
	}

	@Override
	public void onStart() throws Exception {
		LOGGER.info(Statics.HOSTNAME + "\t" + "html buffer start");
	}

	@Override
	public void onStop() throws Exception {
		LOGGER.info(Statics.HOSTNAME + "\t" + "html buffer stop");
	}

	@Override
	public void flush(Set<Html> buffer) throws IOException {
		List<Put> puts = new ArrayList<Put>();

		for (Html html : buffer) {
			puts.add(html.put());
		}

		buffer.clear();

		HBaseUtil.put(Statics.HBASE_TABLE_SPIDERDATA, puts);

		LOGGER.info("htmlbuffer flush size: " + puts.size());

		puts.clear();

		puts = null;
	}

	@Override
	public long getBufferSize() {
		return config.getHtmlBufferSize();
	}

	@Override
	public long getFlushInternal() {
		return config.getHtmlBufferFlushInternal();
	}

	@Override
	public long getSleepTime() {
		return config.getHtmlBufferSleepTime();
	}

}
